package com.tim.app.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.Topic;
import com.tim.app.server.entry.User;
import com.tim.app.ui.activity.SearchActivity;
import com.tim.app.ui.adapter.search.SearchTopicAdapter;
import com.tim.app.ui.adapter.search.SearchUserAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索的header
 */

public class SearchAllHeadView extends LinearLayout implements View.OnClickListener, LoadMoreHandler {

    private LinearLayout llUser;
    private WrapRecyclerView wrvSearchUser;
    private TextView tvLookAtMoreUser;
    private View vUserLine;
    private SearchUserAdapter userAdapter;
    private List<User> userList;

    private LinearLayout llSubject;
    private WrapRecyclerView wrvSearchSubject;
    private TextView tvLookAtMoreSubject;
    private View vSubjectLine;
    private SearchTopicAdapter subjcetAdapter;
    private List<Topic> subjectList;

    static final int TAG_SIZE = 3;//每组的数量

    public SearchAllHeadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        llUser = (LinearLayout) findViewById(R.id.llUser);
        wrvSearchUser = (WrapRecyclerView) findViewById(R.id.wrvSearchUser);
        tvLookAtMoreUser = (TextView) findViewById(R.id.tvLookAtMoreUser);
        vUserLine = findViewById(R.id.vUserLine);
        llSubject = (LinearLayout) findViewById(R.id.llSubject);
        wrvSearchSubject = (WrapRecyclerView) findViewById(R.id.wrvSearchSubject);
        tvLookAtMoreSubject = (TextView) findViewById(R.id.tvLookAtMoreSubject);
        vSubjectLine = findViewById(R.id.vSubjectLine);

        tvLookAtMoreUser.setOnClickListener(this);
        tvLookAtMoreSubject.setOnClickListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvSearchUser.setLayoutManager(layoutManager);

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getContext());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        wrvSearchSubject.setLayoutManager(layoutManager1);

        userList = new ArrayList<>();
        userAdapter = new SearchUserAdapter(getContext(), userList);
        wrvSearchUser.setAdapter(userAdapter);
        userAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {

            }
        });
        subjectList = new ArrayList<>();
        subjcetAdapter = new SearchTopicAdapter(getContext(), subjectList);
        wrvSearchSubject.setAdapter(subjcetAdapter);
        subjcetAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {

            }
        });
    }

    /**
     * 更新数据
     *
     * @param userListValue
     * @param subjectListValue
     */
    public void setData(final List<User> userListValue, final List<Topic> subjectListValue) {
        if (null != userListValue) {
            llUser.setVisibility(View.VISIBLE);
            if (userListValue.size() < TAG_SIZE) {
                tvLookAtMoreUser.setVisibility(View.GONE);
                vUserLine.setVisibility(View.GONE);
            } else {
                tvLookAtMoreUser.setVisibility(VISIBLE);
                vUserLine.setVisibility(VISIBLE);
            }
            userList.clear();
            userList.addAll(userListValue);
            userAdapter.notifyDataSetChanged();
        } else {
            llUser.setVisibility(View.GONE);
        }

        if (null != userListValue) {
            llSubject.setVisibility(View.VISIBLE);
            if (userListValue.size() < TAG_SIZE) {
                tvLookAtMoreSubject.setVisibility(View.GONE);
                vSubjectLine.setVisibility(View.GONE);
            } else {
                tvLookAtMoreSubject.setVisibility(VISIBLE);
                vSubjectLine.setVisibility(VISIBLE);
            }
            subjectList.clear();
            subjectList.addAll(subjectListValue);
            subjcetAdapter.notifyDataSetChanged();
        } else {
            llSubject.setVisibility(View.GONE);
        }
    }


    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvLookAtMoreUser:
                EventManager.ins().sendEvent(EventTag.DO_SEARCH_JUMP,0,0, SearchActivity.SEARCH_USER);
                break;
            case R.id.tvLookAtMoreSubject:
                EventManager.ins().sendEvent(EventTag.DO_SEARCH_JUMP,0,0, SearchActivity.SEARCH_SUNJECT);
                break;
        }
    }
}
